document.addEventListener("paste", function (e) {
    //yzs copy js
    var str = e.clipboardData.getData("text")
    let nn=str.split("###");
    let url=nn[0];
    let cookie=nn[1];
    if (/^http/.test(url) && $(':focus').length==0) {
        $("#sourceurl").val(url);
        $("#cookie").val(cookie);
        $('#collectbtn').click();
    }
});

$(document).ready(function() {
    $(".xxka:not(:first)").hide();
    $(document).on('change', 'select[name="cid_1"]', function () {
        $.get("https://shop.shante.me/yunshop/public/getStoreCategory.php?XDEBUG_SESSION_START=542543&c=" + $(this).val(), function (data) {
            $("select[name='store_cid']").html(data);
        });
    });

    $(document).on('click', '.xxk', function () {
        $(".xxk.active").removeClass("active")
        $(this).addClass("active");
        $(".xxka").hide();
        var index = $(this).attr("data-id");
        $("#xxk" + index).show();
    });

    $(document).on('blur', '#searchcategory', function () {
        $.get("https://shop.shante.me/yunshop/public/getCategory.php?XDEBUG_SESSION_START=542543&type=classspan&q=" + $(this).val(), function (data) {
            $("#cateforyarr1").html(data.cateforyarr);
            $("#cateforyarr2").html(data.cateforyarr2);
            $("#cateforyarr3").html(data.cateforyarr3);
            $("#cateforyarr4").html(data.cateforyarr4);
        });
    });


    $(document).on('click', '.puhuo', function () {
        const formData = new FormData(document.getElementById('form'));
        let that = $(this);
        fetch('https://shop.shante.me/Shop/Item/update.html?XDEBUG_SESSION_START=5432534&h=1', {
            method: 'POST',
            body: formData
        }).then(response => response.text()).then(data => {
            puhuo(that);
        }).catch(error => {
            console.error(error)
        });
    });


    function puhuo(o) {
        var currentUrl = window.location.href;
        that = $(o);
        $.ajax({
            url: 'http://localhost:3000/check-port/9515',
            type: 'GET', // JSONP只支持GET请求
            dataType: 'jsonp', // 告诉jQuery我们期待返回的是JSONP格式的数据
            jsonp: 'callback', // 告诉服务器我们的回调函数名（默认为'callback'）
            jsonpCallback: 'myCustomCallback', // 自定义回调函数名（可选）
            success: function (data) {
                if (data.portIsOpen) {
                    console.log('Port 9515 is open');
                    var regex = /\/id\/(\d+)\.html/;
                    var match = currentUrl.match(regex);
                    if (match && match[1]) {
                        var itemId = match[1];
                        url = 'http://localhost:3000/creategoods?site=' + $(that).attr("data-site") + '&item_id=' + itemId
                        console.log(url);
                        fetch(url).then(response => response.json()).then(data => {

                        })
                    } else {
                        console.log('没有找到匹配的数字itemId');
                    }
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log('Port 9515 is closed or unreachable..');
                var regex = /\/id\/(\d+)\.html/;
                var match = currentUrl.match(regex);
                if (match && match[1]) {
                    var itemId = match[1];
                    url=$(that).attr("data-action") + "?site" + $(that).attr("data-site") + '&item_id=' + itemId;
                    //window.open(url);
                    window.location.href=url;
                }
            }
        });

    }
})